package com.wuliu.driver.controller;

import com.wuliu.driver.pojo.Card;
import com.wuliu.driver.service.CardService;
import com.wuliu.driver.util.Code;
import com.wuliu.driver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/card")
public class CardController {

    @Autowired
    private CardService cardService;

    @RequestMapping("/selectCard/{id}")
    public Result  selectByCardNumber(@PathVariable Integer id) {
        List<Card> card = cardService.selectByCardNumber(id);
        System.out.println(card);
        if (card != null){
            return new Result(Code.GET_OK,card,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }

    @RequestMapping("/insertCard")
    public Result insertCard(@RequestBody Card card) {
        int i = cardService.insertCard(card);
        if (i > 0){
            return new Result(Code.SAVE_OK,"添加成功");
        }else {
            return new Result(Code.SAVE_ERR,"添加失败");
        }
    }

    @RequestMapping("/deleteCard/{id}")
    public Result deleteCard(@PathVariable Integer id) {
        int i = cardService.deleteByBidInt(id);
        if (i > 0){
            return new Result(Code.DELETE_OK,"删除成功");
        }else {
            return new Result(Code.DELETE_ERR,"删除失败");
        }
    }
}
